/*
 * jquery.fillForm File
 *
 * @link http://riedel-st.de
 * @author Stefan Riedel <sr@riedel-st.de>
 */
;
(function($) {
    $.extend($.fn, {
        fillForm: function(formData) {
            function dataObjectSize(dataObject) {
                var size = 0, key;
                for (key in dataObject) {
                    if (dataObject.hasOwnProperty(key)) size++;
                }
                return size;
            }

            var level = 0;
            var oldId = [];
            function _setAttributeRecursive (dataObject, id) {
                var i = 0;
                var size = dataObjectSize(dataObject);
                oldId[level] = id;
                $.each(dataObject, function(name, data){
                    i++;
                    id = oldId[level] + name;
                    if(typeof(data) == 'object') {
                        id += '_';
                        level++;
                        _setAttributeRecursive(data, id);
                    }
                    else {
                        if(size == i && level > 0) level--;
                        $('#'+id).attr('value', data);
                    }
                });
            }
            return this.each(function(){
                _setAttributeRecursive(formData, '');
            });
        }
    });
})(jQuery);